package model

// query.go
// 用于接受请求参数数据模型

// Query 不知道用来干什么， 先放着
type Query interface {
	Default()
}

// Pagination 用于接收分页请求参数
type Pagination struct {
	Page int `form:"page" binding:"omitempty,min=1"`
	Size int `form:"size" binding:"omitempty,min=1,max=100"`
}

// Default 设置默认分页参数并去除非法值
func (p *Pagination) Default() {
	if p.Page < 1 {
		p.Page = 1
	}

	if p.Size <= 0 || p.Size > 100 {
		p.Size = 7
	}
}

type AllPostsPagination struct {
	Topic string `form:"topic" binding:"omitempty"`
	Sort  string `form:"sort" binding:"omitempty,oneof=recent hot"`
	Pagination
}

// Default 设置 AllPostsPagination 的默认值
func (ap *AllPostsPagination) Default() {
	// 明确调用嵌入的 Pagination 类型的 Default 方法
	ap.Pagination.Default()
	if ap.Topic == "" {
		ap.Topic = "all"
	}
	if ap.Sort == "" {
		ap.Sort = "recent"
	}
}

type SearchPagination struct {
	Q     string `form:"q" binding:"required"`
	Topic string `form:"topic" binding:"omitempty"`
	Sort  string `form:"sort" binding:"omitempty,oneof=recent relevance"`
	Pagination
}

// Default 设置 SearchPagination 的默认值
func (sp *SearchPagination) Default() {
	sp.Pagination.Default()
	if sp.Topic == "" {
		sp.Topic = "all"
	}
	if sp.Sort == "" {
		sp.Sort = "recent"
	}
}
